Skip to main content

Coloring and Matching


Bipartite Graphs​

Definition

The chromatic number of a graph HH, denoted by χ(H)\chi (H), is the smallest integer kk for which the vertices of HH can be colored by kk colors so that adjacent vertices are colored by different colors.

Definition

A 2-colorable graph is called bipartite. Equivalently, GG is bipartite if the vertex set of GG can be split into the disjoint sets AA and BB (the color classes) so that each edge of GG is adjacent to one vertex of AA and one vertex of BB.

Theorem 1​

A graph GG is bipartite if and only if it does not contain a cycle of an odd length.

Proof​

Let us assume that GG contains the odd cycle A1A2⋯A2m+1A_1A_2\cdots A_{2m+1}. Let us assume without loss of generality that A1A_1 is red. Then A2A_2 must be blue, therefore A3A_3 must be red, and so on, and at the end, A2m+1A_{2m+1} must be red, too.

This is not allowed, however, as A1A2m+1A_1 A_{2 m+1} is an edge. To prove the "if" part, let GG be a graph with no odd cycles. Let VV be a vertex of GG, and color VV red. Define the color of any other vertex WW as follows. If the shortest path from VV to WW has even length, then let WW be red, and if the shortest path from VV to WW has odd length, then let WW be blue. We show that this is a good coloring, that is, there are no two adjacent vertices that are the same color.

Let us assume the contrary, by first assuming that PP and QQ are two red vertices that are joined by an edge. Let the shortest path from VV to PP be pp, and let the shortest path from VV to QQ be qq. Then pp and qq both have an even number of edges, so walking from VV through pp to PP, then through PQP Q, then back from QQ through qq to VV, we get a closed walk CC with an odd number of edges. Taking away edges that were used both by pp and qq, this walk CC splits into the union of edge-disjoint cycles. As the total number of edges in these cycles is still odd, there has to be at least one cycle with an odd number of edges, which is a contradiction.

If we assumed instead that both PP and QQ were blue, the same proof would work as the sum of two odd numbers is still even, so CC would still have an odd number of edges.

Theorem 2​

Let GG be a simple bipartite graph on nn vertices. Then GG has at most n2/4n^2 / 4 edges if nn is even, and at most (n2−1)/4\left(n^2-1\right) / 4 edges, if nn is odd

Proof​

Choose GG so that no other simple bipartite graph on nn vertices has more edges than GG. Denote by aa and bb the sizes of the two color classes of GG. It is clear that each vertex of one color class is connected to each vertex of the other color class in GG. Indeed, if there was a missing edge between the two color classes, we could add it to GG, contradicting to our assumption. So GG has ab=a(n−a)a b=a(n-a) edges, and the proof follows from elementary calculus. (One simply has to find the integer a∈[1,n]a \in[1, n] for which the number f(a)=a(n−a)f(a)=a(n-a) is maximal.)

Lemma​

Let HH be a simple graph on 2m2 m vertices (m≥2)(m \geq 2) and at least m2+1m^2+1 edges. Then HH contains a triangle.

Proof​

We prove our statement by induction on mm. If m=2m=2, then HH is a subgraph of K4K_4 with at least five edges. Theorem 11.6 shows that HH is not bipartite, so it must have an odd cycle. This odd cycle must be a triangle as HH has only four vertices.

Now assume we know that the statement is true for all integers that are smaller than mm, and are at least 2. Let HH be as in the statement of the Theorem, and let FF and GG be two adjacent vertices in HH. If the sum of the degrees of FF and GG is more than 2m2 m, then they have a common neighbor TT, and so FGTF G T is a triangle. If, on the other hand, the sum of the degrees of FF and GG is at most 2m2 m, then deleting F,GF, G, and all the edges adjacent to them from HH will decrease the number of edges in our graph by at most 2m−12 m-1. (Note that the edge FGF G is contained twice in the sum of the two degrees.) Therefore, after the deletion of these vertices and edges, we are left with a graph of 2m−22 m-2 vertices, and at least m2+1−(2m−1)=m^2+1-(2 m-1)= m2−2m+2=(m−1)2+1m^2-2 m+2=(m-1)^2+1 edges. Such a graph contains a triangle by the induction hypothesis, so our claim is proved.

Theorem 3​

Let HH be a simple graph on 2m2m vertices (m≥2)(m \geq 2) and at least m2+1m^2+1 edges. Then HH contains at least mm triangles.

Proof​

  1. If x≥m−1x \geq m-1, then we are done as we found our missing m−1m-1 triangles.

  2. If 1≤x<m−11 \leq x<m-1, then the total number of edges between ABCA B C and the outside vertices is at most (2m−3)+(m−2)=3m−5(2 m-3)+(m-2)=3 m-5. As ABCA B C itself contains three edges, it follows that there are at least m2+1−(3m−5)−3=m2−3m+3=(m−1)(m−2)+1m^2+1-(3 m-5)-3=m^2-3 m+3=(m-1)(m-2)+1 edges within the subgraph RR spanned by all 2m−32 m-3 outside vertices. If we omit the vertex of RR which has the smallest degree in RR, it follows by the Pigeon-hole Principle that we get a graph R′R^{\prime} on 2m−42 m-4 vertices that still has more than

    (m−1)(m−2)⋅2m−42m−3=(m−2)2⋅2m−22m−3>(m−2)2(m-1)(m-2) \cdot \frac{2 m-4}{2 m-3}=(m-2)^2 \cdot \frac{2 m-2}{2 m-3}>(m-2)^2

    edges. So R′R^{\prime} has strictly more than (m−2)2(m-2)^2 edges, that is, it has at least (m−2)2+1(m-2)^2+1 of them. Therefore, by the induction hypothesis, there are at least m−2m-2 triangles within R′R^{\prime}. As we said in the previous paragraph, there are xx triangles spanned by two vertices of ABCA B C and an outside vertex. In our case, x≥1x \geq 1, so we have again found the m−1m-1 needed triangles.

  3. Finally, consider the case when the number of edges connecting outside vertices to ABCA B C is not more than 2m−32 m-3. Note that we can assume that there is at least one such edge, otherwise RR has m2−2m^2-2 edges, so adding any vertex of ABCA B C to RR creates a graph on 2m−22 m-2 vertices and m2−2≥(m−1)2+1m^2-2 \geq(m-1)^2+1 vertices, and the proof follows by the induction hypothesis. That said, the number of edges within RR is at least m2+1−(2m−3)−3=(m−1)2m^2+1-(2 m-3)-3=(m-1)^2. Adding a vertex of ABCA B C that is adjacent to at least one outside vertex to RR creates a graph with 2m−22 m-2 vertices and at least (m−1)2+1(m-1)^2+1 edges, and again, the induction hypothesis shows that such a graph must contain at least m−1m-1 triangles.

Matchings​

Definition

Let GG be any graph, and let SS be a set of edges in GG so that no two edges in GG have a vertex in common. Then we say that SS is a matching in GG. If each vertex in GG is covered by an edge in SS, then we call SS a perfect matching.

Philip Hall's Theorem​

Let G=(X,Y)G=(X,Y) be a bipartite graph. Then XX has a perfect matching into YY if and only if for all T⊆XT \subseteq X, the inequality ∣T∣≤∣N(T)∣|T| \leq |N(T)| holds.

Proof​

We prove the statement by induction on ∣X∣|X|, the initial case being trivial. Now assume we know the statement for all nonnegative integers less than ∣X∣|X|, and prove it for ∣X∣|X|. Let us assume that for all T⊆XT \subseteq X, the inequality ∣T∣≤∣NG(T)∣|T| \leq\left|N_G(T)\right| holds. We distinguish two cases.

  1. First let us assume that for each proper subset T⊂XT \subset X, even the strict inequality ∣T∣<∣NG(T)∣|T|<\left|N_G(T)\right| holds. Let xx and yy be adjacent vertices, with x∈Xx \in X. Let G′=G−x−yG^{\prime}=G-x-y, and let AA be any nonempty subset of X−xX-x. Our assumption then shows that ∣A∣<∣NG(A)∣|A|<\left|N_G(A)\right|, therefore ∣NG′(A)∣≥∣NG(A)∣−1≥∣A∣\left|N_{G^{\prime}}(A)\right| \geq\left|N_G(A)\right|-1 \geq|A|. Consequently, the induction hypothesis implies that X−xX-x can be matched into Y−yY-y in G′G^{\prime}. Adding the edge xyx y to this matching, we get a perfect matching of XX into YY.

  2. Now assume there is a subset B⊂XB \subset X so that ∣B∣=∣NG(B)∣|B|=\left|N_G(B)\right| holds. We split GG into two smaller subgraphs G1G_1 and G2G_2, and then show that each of these subgraphs satisfies the induction hypothesis separately. Let G1G_1 be the subgraph induced by B∪N(B)B \cup N(B), and let G2G_2 be the graph obtained from GG by deleting all vertices that belong to B∪N(B)B \cup N(B).

To see that G1G_1 satisfies the induction hypothesis, choose any subset T⊆BT \subseteq B. Then NG(T)⊆NG(B)N_G(T) \subseteq N_G(B), and therefore, NG1(T)=NG(T)N_{G_1}(T)=N_G(T), (all neighbors of TT are within G1)\left.G_1\right), and therefore, ∣NG1(T)∣=∣NG(T)∣≥\left|N_{G_1}(T)\right|=\left|N_G(T)\right| \geq ∣T∣|T|.

To see that G2G_2 satisfies the induction hypothesis, choose any subset U⊆X−BU \subseteq X-B. Then NG(U∪B)=NG2(U)∪NG(B)N_G(U \cup B)=N_{G_2}(U) \cup N_G(B), and because this is a union of disjoint sets, ∣NG2(U)∣=∣NG(U∪B)∣−∣NG(B)∣≥\left|N_{G_2}(U)\right|=\left|N_G(U \cup B)\right|-\left|N_G(B)\right| \geq ∣U∪B∣−∣B∣=∣U∣|U \cup B|-|B|=|U|.

If we apply the induction hypothesis to both G1G_1 and G2G_2, we see that BB can be matched into (and therefore, onto), NG(B)N_G(B), and X−BX-B can be matched into Y−NG(B)Y-N_G(B). Therefore, XX can be matched into YY as claimed.

Definition

In a graph GG, a matching MM is called maximal if we cannot extend MM by adding a new edge to it. A matching NN is called maximum if no matchings of GG contain more edges than NN.

Definition

Let GG be a graph, and let MM be a matching in GG. A path P=v1v2⋯vrP=v_1 v_2 \cdots v_r is called an MM-alternating path if vivi+1v_i v_{i+1} is in MM if and only if vi+1vi+2v_{i+1} v_{i+2} is not in MM.

Theorem 2​

Let GG be any simple graph, and let MM be a matching in GG. Then MM is maximum if and only if GG has no MM-augmenting paths.

Proof​

To prove the "if" part, assume there is no MM-augmenting path in GG, and let M′≠MM^{\prime} \neq M be any maximum matching in GG. Consider M⊕M′M \oplus M^{\prime}, the set of edges that are part of exactly one of MM and M′M^{\prime}. As MM and M′M^{\prime} are both matchings, the connected components of M⊕M′M \oplus M^{\prime} can only be even cycles or alternating paths. However, M′M^{\prime} is maximum, and there is no MM augmenting path, therefore all these alternating paths are of even length. This implies ∣M∣=∣M′∣|M|=\left|M^{\prime}\right|, and our claim is proved.

Stable Matchings​

Definition

Let G(X,Y)G(X, Y) be a bipartite graph with a perfect matching MM, with each vertex of XX given an ordered list of preferences for the vertices of YY, and each vertex of YY given an ordered list of preferences for the vertices of XX. We say that MM is stable if the following two conditions do not both hold.

  1. There is a vertex x∈Xx \in X so that xy∈Mx y \in M, but xx prefers y′∈Yy^{\prime} \in Y to yy, and
  2. if y′y^{\prime} and xx are the vertices defined in part (a), and y′x′∈My^{\prime} x^{\prime} \in M, then y′y^{\prime} prefers xx over x′x^{\prime}

Gale-Shapley Algorithm​

The algorithm starts by having each element in one set (traditionally the men) propose to their most preferred element in the other set (traditionally the women). Then, each element in the other set (traditionally the women) reviews their proposals and tentatively accepts the proposal from their most preferred proposer.

If a woman receives a proposal from a man who is not her most preferred, she will reject the proposal and continue to consider other proposals. If a man's proposal is rejected, he will move on to his next most preferred choice and propose again. This process continues until all stable matches have been made.

A matching is considered stable if there are no two pairs where one person in each pair prefers the other person to their current partner. The Gale-Shapley algorithm guarantees that a stable matching will always be found, regardless of the preferences of the individuals involved.

Code Implementation​

def gale_shapley(men, women):
# Initialize all men and women to be free
free_men = men.keys()
free_women = set(women.keys())

# Initialize an empty dictionary to store the matches
matches = {}

# While there are free men, continue proposing
while free_men:
# Choose a free man
man = free_men.pop()

# Get the man's preference list
pref_list = men[man]

# Propose to each woman on the list
for woman in pref_list:
if woman in free_women:
# The woman is free, so they become a match
matches[man] = woman
free_women.remove(woman)
break
else:
# The woman is already matched, so check if she prefers this man over her current partner
current_man = matches[woman]
woman_pref_list = women[woman]
if woman_pref_list.index(man) < woman_pref_list.index(current_man):
# The woman prefers this man, so they become a match and the previous match is freed
matches[man] = woman
free_men.add(current_man)
matches[current_man] = None
break

return matches